package com.gxa.jjty.phy.controller;

import com.gxa.jjty.core.common.ExceptionCode;
import com.gxa.jjty.core.common.JsonResult;
import com.gxa.jjty.core.exception.BusinessException;
import com.gxa.jjty.core.pojo.PhyUser;
import com.gxa.jjty.core.pojo.Torder;
import com.gxa.jjty.core.util.BaiduMapUtil;
import com.gxa.jjty.core.util.LocationUtil;
import com.gxa.jjty.core.util.StringUtil;
import com.gxa.jjty.phy.service.PhyUserService;
import com.gxa.jjty.phy.service.ToderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Map;

/**
 * @ClassName PhyUserController
 * @Author: ws
 * @date 2021/12/6 9:31
 */
@RestController
@RequestMapping("/phyUser")
@Api(tags = "前台陪护员用户的接口")
@Validated
public class PhyUserController {

    @Autowired
    private PhyUserService phyUserService;

    @Autowired
    private ToderService toderService;

    @PostMapping("/login")
    @ApiOperation("用户登录的接口(用户名/密码)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName" ,value = "用户名称",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "password" ,value = "用户密码",paramType = "query",dataType = "String")
    })
    public Map login(@NotEmpty(message = "用户名不能为空") @RequestParam("userName") String userName,
                     @NotEmpty(message = "密码不能为空") @RequestParam("password") String password) throws BusinessException {


        Map login = phyUserService.login(userName, password);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(login);

        return jsonResult.getDataMap();
    }

    @PostMapping("/loginByPhone")
    @ApiOperation("用户登录的接口(手机号验证码一键登录)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone" ,value = "手机号",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "code" ,value = "验证短信",paramType = "query",dataType = "String")
    })
    public Map loginByPhone(@NotEmpty(message = "手机号不能为空") @RequestParam("phone") String phone,
                            @NotEmpty(message = "短信验证码不能为空") @Length(message = "请输入一个四位的短信验证码",max = 4)
                            @RequestParam("code") String code) throws BusinessException {

        if (!StringUtil.isPhoneNumber(phone)) {
            throw new BusinessException(ExceptionCode.BAD_PHONE);
        }
//        if (StringUtil.isEmpty(phone)){
//            throw new BusinessException(ExceptionCode.BAD_PHONE);
//        }

        Map login = phyUserService.loginByPhone(phone, code);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(login);

        return jsonResult.getDataMap();
    }

    @PostMapping("/logout")
    @ApiOperation("用户登出的接口")
    @ApiImplicitParam(name = "token",dataType = "String",paramType = "query")
    public Map logout(@RequestParam("token") String token){

        phyUserService.logout(token);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");

        return jsonResult.getDataMap();
    }

    @PostMapping("/changePassword")
    @ApiOperation("修改密码的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" ,value = "当前陪护员id编号",paramType = "query",dataType = "Long"),
            @ApiImplicitParam(name = "password" ,value = "旧密码",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "newPassword" ,value = "新密码",paramType = "query",dataType = "String")
    })
    public Map changePassword(@RequestParam("id") Long id,
                              @RequestParam("password") String password,
                              @RequestParam("newPassword") String newPassword) throws BusinessException {

//        System.out.println(password);
//        System.out.println(newPassword);

        if (StringUtil.isEmpty(password) ) {
            throw new BusinessException(ExceptionCode.PWD_NOT_NULL);
        }
        if (StringUtil.isEmpty(newPassword) ) {
            throw new BusinessException(ExceptionCode.NEWPWD_NOT_NULL);
        }
        if (password.equals( newPassword)){
            throw new BusinessException(ExceptionCode.NOT_REPETITION);
        }

        phyUserService.changePassword(id,password,newPassword);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");

        return jsonResult.getDataMap();
    }

    @PostMapping("/getList")
    @ApiOperation("获取空闲陪护员列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderNumber",value = "订单编号")
    })
    public Map getList(String orderNumber) throws IllegalAccessException {
        PhyUser phyUser = new PhyUser();
        phyUser.setServerState("空闲");
        List<PhyUser> select = phyUserService.select(phyUser);
        Torder torder = new Torder();
        torder.setOrderNumber(orderNumber);
        List<Torder> select1 = toderService.select(torder);
        Torder torder1 = new Torder();
        if(select1!=null){
            torder1 = select1.get(0);
        }
        Map<String, Double> lngAndLat = BaiduMapUtil.getLngAndLat(torder1.getOrderAddress());
        for(int i=0;i<select.size();i++){
            Map<String, Double> lngAndLat1 = BaiduMapUtil.getLngAndLat(select.get(i).getPosition());
            double distance = LocationUtil.getDistance(lngAndLat.get("lat"), lngAndLat.get("lng"), lngAndLat1.get("lat"), lngAndLat1.get("lng"));
            select.get(i).setDistance("距离用户"+distance+"km");
            phyUserService.updateById(select.get(i));
        }
        List<PhyUser> phyUsers = phyUserService.selectByDesc();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(phyUsers);
        jsonResult.setCode("1");
        return  jsonResult.getDataMap();

    }

}
